package com.example.polarity;

import android.R.interpolator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

public class PlanetEmitter extends ViewGroup 
{
	
	public int SunX;
	public int SunWidth;
	Matrix matrix;
	
	public PlanetEmitter(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	public PlanetEmitter(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.setWillNotDraw(false);
		matrix = new Matrix();
		

		for(int i = 7; i >= 1; i--)
		{
			Planets p = new Planets(context,i);
			
			addView(p);
		}
		
		// TODO Auto-generated constructor stub
	}
	
	public PlanetEmitter(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		

		// TODO Auto-generated constructor stub
	}
	
	@Override
	public void onDraw(Canvas canvas)
	{
		 for(int i = 0; i < this.getChildCount(); i++)
		 {
			Planets p = (Planets)this.getChildAt(i);

			if(i == 0 )
			{
				//canvas.drawBitmap(p.resizedPlanet, p.posX, p.posY, null );
				matrix.setRotate(p.rotation,p.width  / 2,p.height / 2);
				matrix.postTranslate(p.posX, p.posY);
				canvas.drawBitmap(p.resizedPlanet, matrix, null );

				 if(p.rotation >= 360)
					 p.rotation = 1;
				 
				 p.rotation += 1.0;
			}
			else
			{
				matrix.setRotate(p.rotation,this.SunX - p.posX,(this.getHeight() / 2)  - p.posY );

				matrix.postTranslate(p.posX, p.posY);
			    canvas.drawBitmap(p.resizedPlanet, matrix, null ); 
			    
				 
				 if(p.rotation >= 360)
					 p.rotation = p.rotationInc;
				 
				 p.rotation += p.rotationInc;
			}
		 } 
		 invalidate();
	}

	@Override
	protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
		// TODO Auto-generated method stub
		
		 int curPosInc = 0;
		
		 for(int i = 0; i < this.getChildCount(); i++)
		 {
			 Planets p = (Planets)this.getChildAt(i);
			 
			 //if first planet then its the sun
			 if(i == 0)
			 {
				 
				 p.width = (int) (this.getWidth() * p.imagePercentage);
				 p.height = (int) (this.getHeight()  * p.imagePercentage);
				 
				 p.resizedPlanet = Bitmap.createScaledBitmap(p.planetImage, p.width, p.height, false);
				 
				 
				 
				 p.posX = (this.getWidth() / 2) - (p.width / 2) ;
				 p.posY = (this.getHeight() / 2) - p.resizedPlanet.getHeight() /2;
				 this.SunX = p.posX + (p.width / 2);
				 this.SunWidth = p.resizedPlanet.getWidth();

			 }
			 else
			 {
				 
				 p.width = (int) (this.getWidth() * p.imagePercentage);
				 p.height = (int) (this.getHeight()  * p.imagePercentage);
				 
				 p.resizedPlanet = Bitmap.createScaledBitmap(p.planetImage, p.width, p.height, false);
				 
				 
			     curPosInc += (this.getWidth() / 2) / 6;
				 p.posX =  curPosInc - (p.resizedPlanet.getWidth() / 2) - (this.SunWidth  / 2); 
				 p.posY = ( (this.getHeight() / 2)  - p.resizedPlanet.getHeight() /2);
				 
				 if(i == 6)
					 p.posX -= p.width;
			
			 }
		 }
		
	}

}
